//
//  HLRtcCallKitUtils.m
//  YueYue
//
//  Created by hulu001 on 2024/1/9.
//

#import "HLRtcCallKitUtils.h"

@implementation HLRtcCallKitUtils


+ (NSString *)generateRequestID {
    NSInteger random = arc4random() % 1000;
    NSString *requestID = [NSString stringWithFormat:@"%.f%zd",[NSDate timeIntervalSinceReferenceDate],random];
    return requestID;
}

+ (NSString *)createUuidString
{
    CFUUIDRef uuid_ref = CFUUIDCreate(NULL);
        
    CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);
    
    NSString *uuid = [NSString stringWithString:(__bridge NSString *)uuid_string_ref];
    
    CFRelease(uuid_ref);
    
    CFRelease(uuid_string_ref);
    
    return [uuid lowercaseString];
}

-(NSString*) uuid {

    char data[10];
    
    for (int x=0;x<10;data[x++] = (char)('A' + (arc4random_uniform(26))));
    
    NSDate *  datenow=[NSDate date];
    
    NSString *ns_sendCount = [[NSUserDefaults standardUserDefaults] objectForKey:@"sendMessageCount"];
    int sendCount = [ns_sendCount intValue];
    NSDate *preSendTime = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"preSendTime"];
    
    if (preSendTime == nil)
    {
        [[NSUserDefaults standardUserDefaults] setObject:datenow forKey:@"preSendTime"];
    }
    else
    {
        NSCalendar *chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        
        NSUInteger unitFlags =  NSCalendarUnitHour | NSCalendarUnitMinute |NSCalendarUnitSecond | NSCalendarUnitDay| NSCalendarUnitMonth | NSCalendarUnitYear;
        
        NSDateComponents *DateComponent = [chineseClendar components:unitFlags fromDate:preSendTime toDate:datenow options:0];
        
        if ([DateComponent day] > 0)
        {
            [[NSUserDefaults standardUserDefaults] setObject:datenow forKey:@"preSendTime"];
            [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"sendMessageCount"];
            sendCount = 0;
        }
        else
        {
            sendCount ++;
            [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%d",sendCount] forKey:@"sendMessageCount"];
        }
        
    }
    
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
    
    NSString * n_str = [NSString stringWithFormat:@"%@%@%@",
                        [[NSString alloc] initWithBytes:data length:10 encoding:NSUTF8StringEncoding]
                        ,timeSp
                        ,[NSString stringWithFormat:@"%d",sendCount]];
    
    return n_str;
}

@end
